﻿
/*
 * GET home page.
 */
const ajax = require('../models/ajax/ajax');

const xml2js = require('xml2js');
exports.index = function (req, res) {

    switch (req.params[0].toLowerCase()) {



        case "/m/customerlist": //客户维护列表
            res.render('./Manage/Customer/list', { title: '客户维护列表', line: "<tr><td>尼玛哦. hello word!</td></tr>", t1: "客户列表", t2: "可以对客户信息进行查询和维护" });
            break;
        case "/m/customerinfo": //客户详情
            res.render('./Manage/Customer/info', { title: '客户编辑', t1: "编辑客户", t2: "基本信息" });
            break;
        case "/m/login": //用户端登录
            res.render('./Manage/Login', { title: '用户登录', t1: "编辑客户", t2: "基本信息" });
            break;



    }

    res.end();

};



exports.file = function (req, res) {

    var file = require("../models/comm/file");


    file.up(req, res);


}
exports.api = function (req, res) {
    // body...
    //var ajaxClass = req.body.class.toLowerCase()

    res.writeHead(200, {
        "Content-Type": "text/plain;charset=utf-8",
        "Access-Control-Allow-Origin": "http://uzor001.com,http://uzor000.com"
    });

    const mongo = require('../models/mongo/mongo');
    mongo.findOne({
        data: {

            _id: "592c65ddbda7e3b9f90793f5"
        },
        collection: "customerOrder",
        fun: function (err, data) {
            delete data.info._id
            var builder = new xml2js.Builder();  // JSON->xml
            var re = builder.buildObject(data.info);
            res.write(re);
            res.end();
        }


    })


};

exports.ajax = function (req, res) {


    //提交ajax请求
    var ajaxClass = req.body.class.toLowerCase()
    if (!req.body.returnFormat) {
        req.body.returnFormat = "json"
    }

    var ModelPath = "";
    switch (ajaxClass) {
        case "customer":
            ModelPath = "customer/customer";

            break;
        case "customerorder":
            ModelPath = "Order/customerOrder";
            break;
        case "company":
            ModelPath = "company/company";
            break;
        case "member":
            ModelPath = "member/member";
            break;
        case "flow":
            ModelPath = "flow/flow";
            break;
        case "task":
            ModelPath = "task/task";
            break;
        case "comm":
            ModelPath = "comm/bComm";
            break;
        case "contract":
            ModelPath = "contract/bContract";
  
            break;
        default:

            throw "没有找到class:" + ajaxClass + ""
    }
    var ajaxModel = require("../models/" + ModelPath);
    var hello = new ajaxModel(req, res, reAjax, catchFun);


    function catchFun(err) {
        //     console.log(JSON.stringify(err));
        ajax.re(res, {

            err: err
        })
    }

    function reAjax(val) {
        if (!val) {
            val = {};
        }

        val.re = "ok"
        ajax.re(res, {

            val: val
        })
    }











    //   console.log("激活ajax!");



}